<?php
/** 
 * Plugin 'rdt_img' for the 'ameos_formidable' extension.
 *
 * @author	Jerome Schneider <typo3dev@ameos.com>
 */

require_once(PATH_t3lib . "class.t3lib_basicfilefunc.php");

class tx_rdtimg extends formidable_mainrenderlet {

	function _render() {
		return $this->_renderReadOnly();
	}
	
	function _renderReadOnly() {
		
		$sPath = $this->_getPath();

		if(
			$sPath !== FALSE || (
				is_array($this->_navConf("/imageconf/")) &&
				$this->defaultFalse("/imageconf/forcegeneration")
			)
		) {

			$sTag = FALSE;
			$aSize = FALSE;

			if($this->oForm->isAbsServerPath($sPath)) {
				$sAbsServerPath = $sPath;
				$sRelWebPath = $this->oForm->_removeStartingSlash($this->oForm->toRelPath($sAbsServerPath));
				$sAbsWebPath = $this->oForm->toWebPath($sRelWebPath);
				$sFileName = basename($sRelWebPath);
				$aSize = @getImageSize($sAbsServerPath);
			} else {
				if(!$this->oForm->isAbsWebPath($sPath)) {
					// relative web path given
						// turn it into absolute web path
					$sPath = $this->oForm->toWebPath($sPath);
					$aSize = @getImageSize($this->oForm->toServerPath($sPath));
				}

				// absolute web path

				$aInfosPath = parse_url($sPath);
				$aInfosFile = t3lib_div::split_fileref($sPath);

				$sAbsServerPath = "";
				$sRelWebPath = $this->oForm->_removeStartingSlash($aInfosPath["path"]);
				$sAbsWebPath = $sPath;
				$sFileName = $aInfosFile["file"];
			}

			$aHtmlBag = array(
				"filepath" => $sAbsWebPath,
				"filepath." => array(
					"rel" => $sRelWebPath,
					"web" => $this->oForm->toWebPath($this->oForm->toServerPath($sRelWebPath)),
					"original" => $sAbsWebPath,
					"original." => array(
						"rel" => $sRelWebPath,
						"web" => $this->oForm->toWebPath($sAbsServerPath),
						"server" => $sAbsServerPath,
					)
				),
				"filename" =>$sFileName,
				"filename." => array(
					"original" => $sFileName,
				)
			);

			if($aSize !== FALSE) {
				$aHtmlBag["filesize."]["width"] = $aSize[0];
				$aHtmlBag["filesize."]["width."]["px"] = $aSize[0] . "px";
				$aHtmlBag["filesize."]["height"] = $aSize[1];
				$aHtmlBag["filesize."]["height."]["px"] = $aSize[1] . "px";
			}

			if(is_array($mConf = $this->_navConf("/imageconf/")) && array_key_exists("userobj", $mConf)) {

				require_once(PATH_t3lib . "class.t3lib_stdgraphic.php");
				require_once(PATH_tslib . "class.tslib_gifbuilder.php");
				// expecting typoscript

				$aParams = array(
					"filename" => $sFileName,
					"abswebpath" => $sAbsWebPath,
					"relwebpath" => $sRelWebPath,
				);

				if($this->oForm->oDataHandler->aObjectType["TYPE"] == "LISTER") {
					$aParams["row"] = $this->oForm->oDataHandler->__aListData;
				} elseif($this->oForm->oDataHandler->aObjectType["TYPE"] == "DB") {
					$aParams["row"] = $this->oForm->oDataHandler->_getStoredData();
				}

				$this->oForm->_callUserObj(
					$mConf,
					$aParams,
					FALSE,
					$this
				);

				$aImage = array_pop($this->oForm->aLastTs);

				if($this->_defaultFalse("/imageconf/generatetag") === TRUE) {
					$sTag = $GLOBALS["TSFE"]->cObj->IMAGE($aImage);
				} else {
					$sTag = FALSE;
				}

				$sNewPath = $GLOBALS["TSFE"]->cObj->IMG_RESOURCE($aImage);	// IMG_RESOURCE always returns relative path

				$aHtmlBag["filepath"] = $this->oForm->toWebPath($sNewPath);
				$aHtmlBag["filepath."]["rel"] = $sNewPath;
				$aHtmlBag["filepath."]["web"] = $this->oForm->toWebPath($this->oForm->toServerPath($sNewPath));
				$aHtmlBag["filename"] = basename($sNewPath);

				$aNewSize = @getImageSize($this->oForm->toServerPath($sNewPath));

				$aHtmlBag["filesize."]["width"] = $aNewSize[0];
				$aHtmlBag["filesize."]["width."]["px"] = $aNewSize[0] . "px";
				$aHtmlBag["filesize."]["height"] = $aNewSize[1];
				$aHtmlBag["filesize."]["height."]["px"] = $aNewSize[1] . "px";
			}

			$sLabel = $this->getLabel();

			if($sTag === FALSE) {
				if(isset($aHtmlBag["filesize."]["width"])) {
					$sWidth = " width='" . $aHtmlBag["filesize."]["width"] . "' ";
				}

				if(isset($aHtmlBag["filesize."]["height"])) {
					$sHeight = " height='" . $aHtmlBag["filesize."]["height"] . "' ";
				}

				$aHtmlBag["imagetag"] = "<img src=\"" . $aHtmlBag["filepath"] . "\" id=\"" . $this->_getElementHtmlId() . "\" " . $this->_getAddInputParams() . " " . $sWidth . $sHeight . "/>";
				#print_r($aHtmlBag["imagetag"]);
			} else {
				$aHtmlBag["imagetag"] = $sTag;
			}

			$aHtmlBag["__compiled"] = $this->_displayLabel($sLabel) . $aHtmlBag["imagetag"];

			return $aHtmlBag;
		}

		return "";
	}

	function _getPath() {
		
		if(($sPath = $this->_navConf("/path")) !== FALSE) {
			$sPath = $this->_processPath($sPath);
		}

		if($this->oForm->isAbsWebPath($sPath)) {
			return $sPath;
		} else {

			if(($mFolder = $this->_navConf("/folder")) !== FALSE) {
				if($this->oForm->isRunneable($mFolder)) {
					$mFolder = $this->callRunneable($mFolder);
				}
				
				$sValue = $this->getValue();
				$sPath = $this->oForm->_trimSlashes($mFolder) . "/" . $sValue;
			}

			$sFullPath = $this->oForm->toServerPath($sPath);

			if(!file_exists($sFullPath) || !is_file($sFullPath) || !is_readable($sFullPath)) {
				if(($sDefaultPath = $this->_navConf("/defaultpath")) !== FALSE) {

					$sDefaultPath = $this->_processPath($sDefaultPath);
					$sFullDefaultPath = $this->oForm->toServerPath($sDefaultPath);

					if(!file_exists($sFullDefaultPath) || !is_file($sFullDefaultPath) || !is_readable($sFullDefaultPath)) {
						return FALSE;
					} else {
						return $sDefaultPath;
					}
				}

				return FALSE;
			} else {
				return $sFullPath;
			}
		}

		return $sPath;
	}

	function _processPath($sPath) {
		
		if($this->oForm->isRunneable($sPath)) {
			$sPath = $this->callRunneable($sPath);
		}

		if(t3lib_div::isFirstPartOfStr($sPath, "EXT:")) {
			
			$sPath = $this->oForm->_removeStartingSlash(
				$this->oForm->toRelPath(
					t3lib_div::getFileAbsFileName($sPath)
				)
			);
		}

		return $sPath;
	}
	
	function _renderOnly() {
		return TRUE;
	}

	function _readOnly() {
		return TRUE;
	}

	function _getHumanReadableValue($data) {
		return $this->_renderReadOnly();
	}

	function _activeListable() {		// listable as an active HTML FORM field or not in the lister
		return $this->oForm->_defaultTrue("/activelistable/", $this->aElement);
	}

	function getFullServerPath($sPath) {
		return $this->oForm->toServerPath($sPath);
	}

	function _getAddInputParamsArray() {

		$aAddParams = parent::_getAddInputParamsArray();
		
		if(($mUseMap = $this->_navConf("/usemap")) !== FALSE) {
			
			if($this->oForm->isRunneable($mUseMap)) {
				$mUseMap = $this->callRunneable($mUseMap);
			}

			if($mUseMap !== FALSE) {
				$aAddParams[] = " usemap=\"" . $mUseMap . "\" ";
			}
		}

		if(($mAlt = $this->_navConf("/alt")) !== FALSE) {
			
			if($this->oForm->isRunneable($mAlt)) {
				$mAlt = $this->callRunneable($mAlt);
			}

			if($mAlt !== FALSE) {
				$aAddParams[] = " alt=\"" . $mAlt . "\" ";
			}
		}

		reset($aAddParams);
		return $aAddParams;
	}
}


	if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdt_img/api/class.tx_rdtimg.php"])	{
		include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdt_img/api/class.tx_rdtimg.php"]);
	}

?>
